package com.doctor.ui.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

import com.doctor.comm.TextUtil;
import com.doctor.ui.BaseActivity;
import com.doctor.ui.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler{
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//去除标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.blank);
		MyWXApi.getWXApi().handleIntent(getIntent(), this);
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		MyWXApi.getWXApi().handleIntent(intent, this);
	}
	

	/**
	 * 向微信发送请求回调
	 */
	@Override
	public void onReq(BaseReq arg0) {
		// TODO Auto-generated method stub
	}

	
	
	/**
	 * 微信响应回调
	 */
	@Override
	public void onResp(BaseResp resp) {
		// TODO Auto-generated method stub
		if(resp == null) {
			finish();
		} else if(resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
			//验证
			onRespAuth((SendAuth.Resp) resp);
		} else if(resp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
			//微信邀请信息
			if(resp.errCode == BaseResp.ErrCode.ERR_OK) {
				
			}
			finish();
		}
	}
	
	
	/**
	 * 响应验证
	 */
	private void onRespAuth(SendAuth.Resp authResp) {
		if(authResp.errCode != BaseResp.ErrCode.ERR_OK) {
			//用户取消
			finish();
			return;
		}
		
//		if(!authResp.state.equals(MyWXAPI.REQ_STATE)) {
//			//认为不是本应用发起的请求不予处理
//			finish();
//		    return;
//		}
//		String code = authResp.code;
//		if(TextUtil.isEmpty(code)) {
//			
//		} else {
//			getToken(code);
//		}
	}//end onRespAuth

}
